const {getUserInfobyCheckLogin,getAllUsersBySearch,getAllRoles,getAllusers} = require('../services/userService');
const {searchPageSize} = require('../config/config')

module.exports ={
    getUserLogin:async(ctx,next)=>{
        console.log('Login request post 222')
        console.log(ctx.request.url)
        console.log(ctx.request.body)
        const {usernum,password} = ctx.request.body;
        console.log(usernum+"-------"+password)
        let findUser =await getUserInfobyCheckLogin(usernum,password);
        if(findUser===null){
         console.log('账号或密码错误')
         ctx.session.info="账号或密码错误"
         ctx.response.redirect('/login')        
        }else{     
          if(findUser){
            console.log('登录成功')
            ctx.session.userName=findUser.userName;
            ctx.response.redirect('/users/welcome')
          }else{    
           ctx.session.info="系统服务异常，请稍后再试"
           ctx.response.redirect('/login')
          }
        } 
    },
    getUserManage:async(ctx,next)=>{
        console.log("session中的userName:"+ctx.session.userName)
        let users = null;
        // let users = await getAllusers();
        let roles = await getAllRoles();
        console.log(users);
        console.log(roles);
        await ctx.render('userManage',{
          userName:ctx.session.userName,
          users:users,
          roles:roles,
          pageCount:0,
          pageSize:searchPageSize,
          sindex:1
        })
    },
    getSearchUserManage:async(ctx,next)=>{
      const {sno,sname,ssex,srole,sindex} = ctx.request.body;
      console.log(sno,sname,ssex,srole,sindex);
      let roles = await getAllRoles();
      let seachResult = await getAllUsersBySearch(sno,sname,ssex,srole);
      await ctx.render('userManage',{
          userName:ctx.session.userName,
          users:seachResult.users,
          roles:roles,
          pageCount:seachResult.pageCount,
          pageSize:searchPageSize,
          sindex:sindex
      })
    }

}